package com.goldze.mvvmhabit.utils;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.goldze.mvvmhabit.R;

import me.goldze.mvvmhabit.utils.Utils;

/**
 * Glide加载图片
 */
public class GlideUtil {

    @SuppressLint("CheckResult")
    public static RequestBuilder<Drawable> newCacheInstance(String url){
        return Glide.with(Utils.getContext()).load(url)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .apply(RequestOptions.placeholderOf(R.drawable.common_glide_replace));
    }

    public static void newCache(String url,int imageW,int imageH){
        Glide.with(Utils.getContext()).load(url).submit(imageW, imageH);
    }

    public static void newCacheInstance(final String url, final ImageView imageView){
        newCacheInstance(url,R.drawable.common_glide_replace,imageView);
    }

    public static void newCacheInstance(String url,int placeholderRes, ImageView imageView){
        Glide.with(Utils.getContext()).load(url)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .apply(RequestOptions.placeholderOf(placeholderRes))
                .into(imageView);
    }

    @SuppressLint("CheckResult")
    public static void newCacheInstance(String url, RequestListener requestListener){
        newCacheInstance(url).addListener(requestListener).submit();
    }

    /**
     * 加载本地资源
     * @param id
     * @param imageView
     */
    public static void newCacheInstance(int id,ImageView imageView){
        Glide.with(Utils.getContext()).load(id)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .into(imageView);
    }
}
